import numpy as np


# 实现与门
# def AND(x1, x2):
#     w1, w2, theta = 0.5, 0.5, 0.7
#     res = w1 * x1 + w2 * x2
#     if res > theta:
#         return 1
#     else:
#         return 0

def AND(x1, x2):
    x = np.array([x1, x2])
    w = np.array([0.5, 0.5])
    b = -0.7
    # 直接用矩阵运算的模式计算结果
    res = x @ w + b
    if (res > 0):
        return 1
    else:
        return 0


# 实现与非门
def NAND(x1, x2):
    x = np.array([x1, x2])
    w = np.array([-0.5, -0.5])
    b = 0.7
    # 直接用矩阵运算的模式计算结果
    res = x @ w + b
    if (res > 0):
        return 1
    else:
        return 0


# 实现或门
def OR(x1, x2):
    x = np.array([x1, x2])
    w = np.array([0.5, 0.5])
    b = -0.2
    # 直接用矩阵运算的模式计算结果
    res = x @ w + b
    if (res > 0):
        return 1
    else:
        return 0


# 实现异或门
def XOR(x1, x2):
    s1 = NAND(x1, x2)
    s2 = OR(x1, x2)
    return AND(s1, s2)


# 测试
print('与门')
print(AND(0, 0))
print(AND(0, 1))
print(AND(1, 0))
print(AND(1, 1))

print('与非门')

print(NAND(0, 0))
print(NAND(0, 1))
print(NAND(1, 0))
print(NAND(1, 1))

print('或门')
print(OR(0, 0))
print(OR(0, 1))
print(OR(1, 0))
print(OR(1, 1))

print('异或门')
print(XOR(0, 0))
print(XOR(0, 1))
print(XOR(1, 0))
print(XOR(1, 1))
